<?php
function form_change_submit($karyawan_id ){
	$new_jabatan_id = check_jabatan_by_no_org($_POST['nama_jabatan']);
	$query = "UPDATE kekaryawanan 
		SET jabatan_gaji_id ={$new_jabatan_id} 
		WHERE karyawan_id = {$karyawan_id}";
	return my_query($query);
}


function form_change_jabatan($karyawan_id , $jabatan_id){
	
	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);
	
	my_set_code_js('  
	function findValue(li) {
		if( li == null ) return alert("No match!"); 
		if( !!li.extra ) var sValue = li.extra[0]; 
		else var sValue = li.selectValue;
	}

	function selectItem(li) {
		findValue(li);
	}

	function formatItem(row) {
		return   row[0] ;
		}
 
	$(document).ready(function() {
		$("#nama_jabatan").autocomplete(
			"autocomplete_daftar_jabatan.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:5,
				onItemSelect:selectItem,
				onFindValue:findValue,
				formatItem:formatItem,
				autoFill:true
			}
		);
	});

	function checkNameJabatan(name){
		$.get("check_jabatan_names.php", { nama: name  },
		function(data){
			if(data ==  \'0\' ){
				alert(\'Jabatan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\'); 
				$(\'#nama_jabatan\').val(\'\');
				return false;
			} 
		});
	}
	');
	
	$view = form_header( "" , ""  );
	
	$karyawan = loaddata_karyawan($karyawan_id);
	$jabatan = loaddata_jabatan($jabatan_id);
	$jabatans = my_get_data_by_id('jabatan' , 'jabatan_id' , $jabatan_id );
	
	$view .= form_field_display( $karyawan['nama']  , "Nama"  );
	$view .= form_field_display( $karyawan['nik']  , "NIK"  ); 
	 
	$nomor_posisi=$jabatans['nomor_posisi']."/".$jabatans['nama_jabatan'] ;
	$jabatan = array(
			'name'=>'nama_jabatan',
			'value'=>(isset($_POST['nama_jabatan'])? $_POST['nama_jabatan'] : $nomor_posisi  ),
			'id'=>'nama_jabatan',
			'type'=>'textfield',
			'size'=>'60',
			'onchange'=>'javascript:checkNameJabatan(this.value)'
		);
	$form_jabatan = form_dynamic($jabatan);
	$view .= form_field_display( $form_jabatan  , "No. Posisi/Jabatan"  );
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}